package com.exiassassin;

import java.io.File;
import java.io.FileOutputStream;
import com.exiassassin.librairies.game.Game;
import com.exiassassin.librairies.game.Player;
import com.model.PlayerImp;
import com.remoteJavaService.CallGameService;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ExiassassinActivity extends Activity implements OnClickListener{
	/** Called when the activity is first created. */

	/* Common Variable */
	private Resources res;
	private Intent intent;

	/* Class Variable */
	private Button buttonConnection;
	private Button buttonTakePicture;
	private EditText editTextLogin;
	private EditText editTextPassword;
	private EditText editTextServerAddress;
	private ImageView imageViewPlayer;
	private PlayerImp player;
	private Game game;
	private LinearLayout linearImageViewPlayer;
	/* Game Class Variable */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);       
		buttonConnection = (Button) findViewById(R.id.buttonConnection);
		buttonConnection.setOnClickListener(this);
		buttonConnection.setEnabled(true);
		buttonTakePicture = (Button) findViewById(R.id.buttonTakePicture);
		buttonTakePicture.setOnClickListener(this);

		imageViewPlayer = (ImageView) findViewById(R.id.imageViewPlayer);
		linearImageViewPlayer = (LinearLayout) findViewById(R.id.linearLayoutImageView);
		/* get Bitmap if exist */
		Bitmap bMap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "tmp_picture.jpg");
		if (bMap != null)
		{
			imageViewPlayer.setImageBitmap(bMap);
			imageViewPlayer.setVisibility(0);
			/* Image should be scaled as width/height are set. */
			imageViewPlayer.setScaleType(ImageView.ScaleType.FIT_CENTER);
            /* Set the Width/Height of the ImageView. */
			imageViewPlayer.setMinimumHeight(110);
			imageViewPlayer.setMinimumWidth(110);
			linearImageViewPlayer.setVisibility(0);
		}
		else
		{
			imageViewPlayer.setVisibility(8);
			linearImageViewPlayer.setVisibility(8);
		}
		editTextLogin = (EditText) findViewById(R.id.editTextLogin);
		editTextPassword = (EditText) findViewById(R.id.editTextPassword);
		editTextServerAddress = (EditText) findViewById(R.id.editTextServerAddress);
		player = new PlayerImp();
	}

	public void onClick(View v) {
		Log.i("ExiassassinActivity","Click");
		switch (v.getId())
		{
		case R.id.buttonConnection:
			Log.i("ExiassassinActivity","buttonConnection - Click Detected");
			/* Fill player before connection */ 
			//player.setLogin(editTextLogin.getText().toString());
			//player.setPassword(editTextLogin.getText().toString());
			player.setLogin("test");
			player.setPassword("testpwd");
			player.setId(1);
			player.setAlive(true);
			/* Execute Task to check connection */
			/*ConnectionTask connectionTask = new ConnectionTask();
			if (connectionTask.execute(player) != null)
			{
					intent = new Intent(this,GameActivity.class);
					startActivity(intent);
					this.finish();
			}
			else
			{
				// Pas connecté
			}*/
			//connectionTask.execute();
			intent = new Intent(this,GameActivity.class);
			startActivity(intent);
			this.finish();
			break;
		case R.id.buttonTakePicture:
			Log.i("ExiassassinActivity","buttonTakePicture - Click Detected");
			intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			try {
				intent.putExtra("return-data", true);
				startActivityForResult(intent, 1000);
			} catch (ActivityNotFoundException e) {
				Log.e("doTakePhotoAction", e.toString());
			}
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK){
			return;
		}
		Log.i("ExiassassinActivity","resultCode - Ok");
		switch (requestCode){
		case 1000:	   
			Log.i("ExiassassinActivity","requestCode - Ok");
			final Bundle extras = data.getExtras();

			if (extras != null) {
				Log.i("ExiassassinActivity","extras - Ok");
				Bitmap photo = extras.getParcelable("data");
				if (photo != null)
				{
					imageViewPlayer.setImageBitmap(photo);
					imageViewPlayer.setVisibility(0);
					linearImageViewPlayer.setVisibility(0);
					buttonConnection.setEnabled(true);
					String filename = "tmp_picture.jpg";
					File sd = Environment.getExternalStorageDirectory();
					File dest = new File(sd, filename);
					try {
					     FileOutputStream out = new FileOutputStream(dest);
					     photo.compress(Bitmap.CompressFormat.PNG, 90, out);
					     out.flush();
					     out.close();
					} catch (Exception e) {
					     e.printStackTrace();
					}
				}
			}
			break;
		}

	}

	private class ConnectionTask extends AsyncTask<PlayerImp, Intent, Player> {
		String s;

		@Override
		protected Player doInBackground(PlayerImp... params) {
			Log.i("ConnectionTask","gameService - en cours...");
			CallGameService gameService = new CallGameService();
			//gameService.setIP_ADRESS(editTextServerAddress.getText().toString());
			Log.i("ConnectionTask","gameService - oK");

			Player playerConnected = gameService.connection(params[0]);
			if (playerConnected != null)
				Log.i("ConnectionTask","playerConnected :"+playerConnected.getLogin());
			else
				Log.i("ConnectionTask","connection échoué");
			return null;
		}

		protected void onPostExecute(Intent... i) {
			/* Execution après connection */
			//    Intent monIntent = new Intent(ExiassassinActivity.this,GameActivity.class);
			 //   startActivity(i[0]);
		}
	}
	
	@Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}